/**
 * game - a lab about webgl, threejs, box2dweb
 */

define('game', ['remote', 'gamelib'], function (remote, gamelib) {
    'use strict';
    
    function Platform() {}
    Platform.prototype = new gamelib.Object;
    Platform.prototype.constructor = Platform;
    Platform.prototype.init = function (x, z) {
        this.object = gamelib.createBox(x, 5, z, 20, 5, 20, {
            map: textures[2],
            specular: 0xffffff,
            shininess: 60,
            friction: 0.01
        });
        this.object.body.SetLinearDamping(0.2);
        this.object.body.SetAngularDamping(0.2);
        return this;
    };
    
    var base_url = 'http://i1212.photobucket.com/albums/cc458/Stephen_Carmody/',
        texture_urls = [
            base_url + 'ground0.jpg',
            base_url + 'ground.jpg',
            base_url + 'platform.jpg'
        ],
        textures = [];
    
    (function loader(img) {
        if (typeof img !== 'undefined') {
            var texture = new gamelib.THREE.Texture(img);
            texture.needsUpdate = true;
            textures.push(texture);
        }
        if (texture_urls.length) {
            remote.img(texture_urls.shift(), loader);
        } else {
            play();
        }
    })();
    
    
    function play() {
        gamelib.scene.addObject(new gamelib.THREE.Mesh(
            new gamelib.THREE.CubeGeometry(300, 1, 300),
            new gamelib.THREE.MeshPhongMaterial({
                map: textures[0]
            })
        ));
        
        gamelib.createBox(-150, 5, 0, 4, 10, 300, {dynamic:false, map: textures[1]});
        gamelib.createBox(150, 5, 0, 4, 10, 300, {dynamic:false, map: textures[1]});
        gamelib.createBox(0, 5, 150, 304, 10, 4, {dynamic:false, map: textures[1]});
        gamelib.createBox(0, 5, -150, 304, 10, 4, {dynamic:false, map: textures[1]});
        
        var p = (new Platform).init(0, 0);
        p.object.body.ApplyForce(
            new gamelib.Box2D.Common.Math.b2Vec2(5000000, -1000000),
            new gamelib.Box2D.Common.Math.b2Vec2(2, 4)
        );
        p.object.body.ApplyForce(
            new gamelib.Box2D.Common.Math.b2Vec2(50000, -1000000),
            new gamelib.Box2D.Common.Math.b2Vec2(2, 4)
        );
        
        (new Platform).init(50, -10);
        
        gamelib.createDirectionalLight(-1, 1, -1, 0xffffff, 0.9);
        gamelib.createPointLight(-100, 100, -100, 0xffffff, 0.1);
        gamelib.createAmbientLight(0x222222);
        gamelib.camera.position.set(-300, 150, 300);
        gamelib.camera.up.set(0, 1, 0);
        gamelib.play();
    }
});
